package com.zgjkhis.modules.system.service.mapper;

import com.zgjkhis.common.mapper.CoreMapper;
import com.zgjkhis.modules.system.domain.DrugWareHousing;
import com.zgjkhis.modules.system.domain.vo.DrugWareHousingDetailVO;
import com.zgjkhis.modules.system.service.dto.DrugWareHousingQueryCriteria;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author yzq
 * @Description 药品入库
 * @Date 2021/6/23 9:48
 * @Param
 * @return
 **/
@Repository
public interface DrugWareHousingMapper extends CoreMapper<DrugWareHousing> {


    @Select("<script>select * from his_drug_warehousing t \n" +
            "where 1=1 and t.pharmacy_id=#{drugWare.pharmacyId} \n" +
            "<if test=\"drugWare.wareHousingType !=null and drugWare.wareHousingType!=''\">\n" +
            "and t.ware_housing_type=#{drugWare.wareHousingType}\n" +
            "</if>\n" +
            "<if test=\"drugWare.supplierId !=null and drugWare.supplierId!=''\">\n" +
            "and t.supplier_id=#{drugWare.supplierId}\n" +
            "</if>\n" +

            "<if test=\"drugWare.status !=null and drugWare.status!=''\">\n" +
            "and t.status=#{drugWare.status}\n" +
            "</if>\n" +



            "<if test=\"drugWare.status == null and drugWare.type !=1 \">\n" +
            "and t.status in ('3','7','8')\n" +
            "</if>\n" +
            "<if test=\"drugWare.wareHousingNo !=null and drugWare.wareHousingNo!=''\">\n" +
            "and t.ware_housing_no=#{drugWare.wareHousingNo}\n" +
            "</if>\n" +
            "<if test=\"drugWare.drugType !=null and drugWare.drugType!=''\">\n" +
            "and t.drug_type=#{drugWare.drugType}\n" +
            "</if>\n" +

            "<if test=\"drugWare.createUserName !=null and drugWare.createUserName!=''\">\n" +
            "and (t.create_user_name LIKE CONCAT('%',#{drugWare.createUserName},'%') )\n" +
            "</if>\n" +

            "<if test=\"drugWare.payStatus != null\">\n" +

            "<choose>"+
            "<when test=\"drugWare.payStatus == ''\"> " +
            "and t.pay_status = 0\n" +
            "</when> " +
            "<otherwise>" +
            "and t.pay_status = #{drugWare.payStatus}\n" +
            "</otherwise> " +
            "</choose>"+
            "</if>\n" +
            "<if test=\"drugWare.createStartTime !=null \">\n" +
            "and t.create_time  &gt; #{drugWare.createStartTime}\n" +
            "</if>\n" +
            "<if test=\"drugWare.createEndTime !=null \">\n" +
            "and t.create_time    &lt;  str_to_date(DATE_ADD(#{drugWare.createEndTime},INTERVAL 1 DAY), '%Y-%m-%d %H')\n" +
            "</if>"+
            "<if test=\"drugWare.startTime !=null \">\n" +
            "and t.warehousing_time  &gt; #{drugWare.startTime}\n" +
            "</if>\n" +
            "<if test=\"drugWare.endTime !=null \">\n" +
            "and t.warehousing_time    &lt;  str_to_date(DATE_ADD(#{drugWare.endTime},INTERVAL 1 DAY), '%Y-%m-%d %H')\n" +
            "</if>\n</script>")
    List<DrugWareHousing> findAllByPageable(@Param("drugWare")DrugWareHousingQueryCriteria criteria);

    @Select("<script>select d.*,dr.drug_type,t.supplier_name from his_drug_warehousing_detail d " +
            "left join his_drug dr on d.drug_id=dr.id "+
            "left join  his_drug_warehousing t on d.ware_housing_no=t.ware_housing_no \n" +
            "where 1=1 and t.pharmacy_id=#{drugWare.pharmacyId} \n" +
            "<if test=\"drugWare.wareHousingType !=null and drugWare.wareHousingType!=''\">\n" +
            "and t.ware_housing_type=#{drugWare.wareHousingType}\n" +
            "</if>\n" +
            "<if test=\"drugWare.supplierId !=null and drugWare.supplierId!=''\">\n" +
            "and t.supplier_id=#{drugWare.supplierId}\n" +
            "</if>\n" +

            "<if test=\"drugWare.status !=null and drugWare.status!=''\">\n" +
            "and t.status=#{drugWare.status}\n" +
            "</if>\n" +



            "<if test=\"drugWare.status == null and drugWare.type !=1 \">\n" +
            "and t.status in ('3','7','8')\n" +
            "</if>\n" +
            "<if test=\"drugWare.wareHousingNo !=null and drugWare.wareHousingNo!=''\">\n" +
            "and t.ware_housing_no=#{drugWare.wareHousingNo}\n" +
            "</if>\n" +
            "<if test=\"drugWare.drugType !=null and drugWare.drugType!=''\">\n" +
            "and t.drug_type=#{drugWare.drugType}\n" +
            "</if>\n" +

            "<if test=\"drugWare.createUserName !=null and drugWare.createUserName!=''\">\n" +
            "and (t.create_user_name LIKE CONCAT('%',#{drugWare.createUserName},'%') )\n" +
            "</if>\n" +

            "<if test=\"drugWare.payStatus != null\">\n" +

            "<choose>"+
            "<when test=\"drugWare.payStatus == ''\"> " +
            "and t.pay_status = 0\n" +
            "</when> " +
            "<otherwise>" +
            "and t.pay_status = #{drugWare.payStatus}\n" +
            "</otherwise> " +
            "</choose>"+
            "</if>\n" +
            "<if test=\"drugWare.createStartTime !=null \">\n" +
            "and t.create_time  &gt; #{drugWare.createStartTime}\n" +
            "</if>\n" +
            "<if test=\"drugWare.createEndTime !=null \">\n" +
            "and t.create_time    &lt;  str_to_date(DATE_ADD(#{drugWare.createEndTime},INTERVAL 1 DAY), '%Y-%m-%d %H')\n" +
            "</if>"+
            "<if test=\"drugWare.startTime !=null \">\n" +
            "and t.warehousing_time  &gt; #{drugWare.startTime}\n" +
            "</if>\n" +
            "<if test=\"drugWare.endTime !=null \">\n" +
            "and t.warehousing_time    &lt;  str_to_date(DATE_ADD(#{drugWare.endTime},INTERVAL 1 DAY), '%Y-%m-%d %H')\n" +
            "</if>\n</script>")
    List<DrugWareHousingDetailVO> findDetailBy(@Param("drugWare")DrugWareHousingQueryCriteria criteria);
}
